package com.liangzhong.proxy.tourMatch
{
	import com.liangzhong.commandconst.tourMatch.CMD_TourMatch;
	import com.liangzhong.global.CMD_Head;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.tourMatch.TourMatchServerDataParse;
	import com.liangzhong.model.tourMatch.vo.TourJoinMatchVo;
	import com.liangzhong.model.tourMatch.vo.TourMatchEndVo;
	import com.liangzhong.model.tourMatch.vo.TourMatchStatusVo;
	import com.liangzhong.proxy.BaseProxy;
	import com.socket.clientsocket.IClientSocket;
	import com.socket.clientsocket.IClientSocketHandler;
	
	import flash.utils.ByteArray;
	
	import lz.controls.Alert;

	/**
	 *巡回赛 
	 * @author lake
	 * 
	 */	
	public class TourMatchProxy extends BaseProxy implements IClientSocketHandler
	{
		public static const ProxyName :String = "TourMatchProxy";
		/**
		 * 获取status result
		 */		
		public static const TOURMATCH_GETSTATUS_RESULT		:String = "Server/TourMatch/TOURMATCH_GETSTATUS_RESULT";
		/**
		 *参赛result 
		 */		
		public static const TOURMATCH_JOINMATCH_RESULT 	:String = "Server/TourMatch/TOURMATCH_JOINMATCH_RESULT";
		/**
		 *比赛开始返回信息 
		 */		
		public static const TOURMATCH_START_DATA_RESULT	:String = "Server/TourMatch/TOURMATCH_START_DATA_RESULT";
		/**
		 *一轮巡回赛结束 
		 */		
		public static const TOURMATCH_END					:String = "Server/TourMatch/TOURMATCH_END";
		
		public function TourMatchProxy(proxyName:String = ProxyName, data:Object=null)
		{
			super(proxyName, data);
		}
		/**
		 * 数据读取
		 * @param mainCmdID
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @param clientSocket
		 * @return 
		 */		
		override public function onSocketRead(mainCmdID:uint,
											  subCmdID:uint,
											  buffer:ByteArray, 
											  dataSize:int, 
											  clientSocket:IClientSocket):Boolean
		{
			switch(mainCmdID)
			{
				case CMD_TourMatch.MAIN:
				{
					return onDeal(mainCmdID,subCmdID,buffer,dataSize);
				}
			}
			return false;
		}
		/**
		 * 处理信息
		 * @param mainCmdID
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @return 
		 * 
		 */		
		private function onDeal(mainCmdID:uint,
								subCmdID:uint,
								buffer:ByteArray, 
								dataSize:int):Boolean
		{
			switch(subCmdID)
			{
				case CMD_TourMatch.GET_STATUS://比赛状态
				{
					var statusVo :TourMatchStatusVo = TourMatchServerDataParse.readTourMatchStatus(buffer);
					AppData.getInstance().tourMatchManager.tourMatchStatus = statusVo;
					AppData.getInstance().tourMatchManager.curLevel = statusVo.level;
					this.sendNotification(TOURMATCH_GETSTATUS_RESULT,statusVo);
					return true;
				}
				case CMD_TourMatch.JOIN_MATCH://参赛返回
				{
					var joinMatchVo :TourJoinMatchVo = TourMatchServerDataParse.readTourJoinMatch(buffer);
					if(joinMatchVo.ret == 0)
					{
						this.sendNotification(TOURMATCH_JOINMATCH_RESULT,joinMatchVo);
					}
					else if(joinMatchVo.ret == 110)
					{
						Alert.showErrorTip("等级不够");
					}
					else
					{
						Alert.showErrorTip("报名失败 code = " + joinMatchVo.ret);
					}
					return true;
				}
				case CMD_TourMatch.START_MATCH://开始比赛
				{
					var ret :int = buffer.readInt();
					if(ret == 0)
					{
//						AppData.getInstance().stadiumDataManager.analysisOpponentmatchInfo(buffer);
//						AppData.getInstance().stadiumDataManager.setSelfMatchInfo();
					}
					this.sendNotification(TOURMATCH_START_DATA_RESULT,ret);
					return true;
				}
				case CMD_TourMatch.TOUR_CUR_MATCH_END://本轮巡回赛结束
				{
					var tourMatchEndVo :TourMatchEndVo = TourMatchServerDataParse.readTourMatchEnd(buffer);
					this.sendNotification(TOURMATCH_END,tourMatchEndVo);
					return true;
				}
			}
			return false;
		}
		/**
		 *获取巡回赛状态 
		 * 
		 */		
		public function getTourMatchStatus():void
		{
			var userId :int = AppData.getInstance().user.uid;
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size,CMD_TourMatch.MAIN,CMD_TourMatch.GET_STATUS,userId);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *报名参加比赛 
		 * @param level 比赛级别
		 * 
		 */		
		public function joinMatchReq(level:int):void
		{
			AppData.getInstance().tourMatchManager.curLevel = level;
			var userId :int = AppData.getInstance().user.uid;
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 1,CMD_TourMatch.MAIN,CMD_TourMatch.JOIN_MATCH,userId);
			sendBuf.writeByte(level);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *比赛开始请求 
		 * @param cid
		 */		
		public function  startMatchReq(cid :int):void
		{
			var userId :int = AppData.getInstance().user.uid;
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 4,CMD_TourMatch.MAIN,CMD_TourMatch.START_MATCH,userId);
			sendBuf.writeUnsignedInt(cid);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
	}
}